package com.ynjtc.desserts.controller;

import com.ynjtc.desserts.common.WrapMapper;
import com.ynjtc.desserts.common.Wrapper;
import com.ynjtc.desserts.entity.Dessert;
import com.ynjtc.desserts.service.DessertService;
import com.ynjtc.desserts.util.ImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @Author: lihua
 * @Date: 2025/9/24 23:17
 * @Version: v1.0.0
 * @Description:
 **/
@RestController
@RequestMapping("/dessert")
@Slf4j
public class DessertsController {

    @Autowired
    private DessertService dessertService;

    @PostMapping("/img")
    public Wrapper<?> uploadImg(@RequestParam("file") MultipartFile file){
        String fileName = ImageUtil.saveImgage(file);
        if( fileName != null )
            return WrapMapper.ok(fileName);
        else
            return WrapMapper.error("上传失败，请重新上传！");
    }
    @PostMapping("/add")
    public Wrapper<?> addDessert(@RequestBody Dessert dessert){
        //todo 参数检查
        Dessert newDessert = dessertService.addDessert(dessert);
        if( newDessert == null)
            return WrapMapper.error("添加错误，请检查后再次尝试");
        else
            return WrapMapper.ok(newDessert);
    }


}
